<template>
    <div>
        <h4>地图控件</h4>
        <div id="container"></div>
        <div class='input-card'>
            <div class="input-item">
                <input type="checkbox" checked onclick="toggleScale(this)" />比例尺
            </div>

            <div class="input-item">
                <input type="checkbox" checked id="toolbar" onclick="toggleToolBar(this)" />工具条
            </div>

            <div class="input-item">
                <input type="checkbox" checked id="controlBar" onclick="toggleControlBar(this)" />工具条方向盘
            </div>

            <div class="input-item">
                <input type="checkbox" checked id="overview" onclick="toggleOverViewShow(this)" />显示鹰眼
            </div>

        </div>
    </div>
</template>

<script setup>
import { onMounted, onUnmounted } from "vue";
import AMapLoader from "@amap/amap-jsapi-loader";

let map = null;

onMounted(() => {
    window._AMapSecurityConfig = {
        securityJsCode: "bfc4628de1efdb7a10d5a97be15d4cc1",
    };
    AMapLoader.load({
        key: "f8688af27c7f44f9904a27c6fa90a510", // 申请好的Web端开发者Key，首次调用 load 时必填
        version: "2.0", // 指定要加载的 JSAPI 的版本，缺省时默认为 1.4.15
        plugins: ["AMap.Scale"], //需要使用的的插件列表，如比例尺'AMap.Scale'，支持添加多个如：['...','...']
    })
        .then((AMap) => {
            map = new AMap.Map("container", {
                viewMode: '3D',
                zoom: 11,
                pitch: 30,
            });
            map.addControl(scale);
            map.addControl(toolBar);
            map.addControl(controlBar);
            map.addControl(overView);
            function toggleScale(checkbox) {
                if (checkbox.checked) {
                    scale.show();
                } else {
                    scale.hide();
                }
            }
            function toggleToolBar(checkbox) {
                if (checkbox.checked) {
                    showToolBar();
                } else {
                    hideToolBar();
                }
            }
            function toggleControlBar(checkbox) {
                if (checkbox.checked) {
                    controlBar.show()
                } else {
                    controlBar.hide()
                }
            }
            function toggleOverViewShow(checkbox) {
                if (checkbox.checked) {
                    overView.show();
                } else {
                    overView.hide();
                }
            }

            function showToolBar() {
                document.getElementById('toolbar').checked = true;
                toolBar.show();
            }
            function hideToolBar() {
                document.getElementById('toolbar').checked = false;
                toolBar.hide();
            }
            function showControlBar() {
                document.getElementById('controlBar').checked = true;
                controlBar.show();
            }
            function hideControlBar() {
                document.getElementById('controlBar').checked = false;
                controlBar.hide();
            }
        })
        .catch((e) => {
            console.log(e);
        });
        
});

onUnmounted(() => {
    map?.destroy();
});
</script>

<style lang="scss" scoped>
html,
body {
    height: 800px;
}

#container {
    height: 800px;
}

.input-card {
    width: 200px;
    top: 70px;
    left: 200px;
     position: absolute;
}
</style>